package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    //新增菜品
    @Override
    @Transactional
    public void savewithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //向菜品表插入一条数据
        dishMapper.insert(dish);  //此处只是向菜品表插入数据 所以并不需要DishDto 因为DishDto中并没有创建时间等的数据

        //获取菜品id
        Long dishId = dish.getId();  //还需要在sql中设置自增主键回填
        //向口味表插入多条数据
        List<DishFlavor> list=dishDTO.getFlavors();
        if(list!=null && list.size()>0){
            list.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });
            dishFlavorMapper.insertBatch(list);
        }
    }

    //菜品分页查询

    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page=dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    //菜品批量删除
    @Override
    public void deleteById(List<Long> ids) {
        //判断当前菜品是否可以删除是否存在正在售卖的菜品
        for(Long id:ids){
            Dish dish =dishMapper.getById(id);
            if(dish.getStatus()== StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //判断当前菜品是否在套餐中--是否在菜品套餐中
        List<Long> setmealIds=setmealDishMapper.getSetmealIdByDishId(ids);
        if(setmealIds!=null && setmealIds.size()>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //根据菜品id集合批量删除菜品
        dishMapper.delectByIds(ids);

        //根据菜品id集合批量删除菜品口味
        dishFlavorMapper.delectByIds(ids);
    }

    //菜品起售停售
    @Override
    public void status(Integer status, Long id) {
        dishMapper.updataStatus(status,id);
    }

    //根据id查询菜品
    @Override
    public DishVO getByIdwithFlavor(Long id) {
        Dish dish =dishMapper.getById(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        //根据菜品id查询菜品口味
        List<DishFlavor> flavors = dishFlavorMapper.getByDishId(id);
        dishVO.setFlavors(flavors);
        return dishVO;
    }

    //根据id菜品修改
    @Override
    public void update(DishDTO dishDTO) {
        //首先修改原本的dish表
       Dish dish = new Dish();
       BeanUtils.copyProperties(dishDTO,dish);
       dishMapper.update(dish);
        //再修改菜品口味
        //先删除原本的口味
        dishFlavorMapper.deleteById(dishDTO.getId());
        //再添加新的口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors!=null && flavors.size()>0){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishDTO.getId());
            });
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    //选择菜品分类中的某个菜品用于套餐分类中新增套餐时选择要新增的菜品
    @Override
    public List<Dish> list(Long categoryId) {
        List<Dish> list =dishMapper.getcategoryId(categoryId);
        return list;
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.getcategoryId(dish.getCategoryId());

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
